package auto.device;

import io.appium.java_client.AppiumDriver;


import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;



public class Phione {
    private URL url = null;
    private DesiredCapabilities caps = null;
    public AppiumDriver driver;
    public WebDriverWait wait;

    //appium --use-plugin=images

    //显式等待时间,15秒内没有出现相应的元素就报错
    private int EXPLICITTIME = 15;

    public Phione() {
        try {
            url = new URL("http://localhost:4723");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        // 获取设备信息
        String udid = getDeviceUdid();
        String platformVersion = getPlatformVersion();
        String deviceName = getDeviceName();

        // 调试输出设备信息
        System.out.println("Device UDID: " + udid);
        System.out.println("Platform Version: " + platformVersion);
        System.out.println("Device Name: " + deviceName);

        // 配置 iOS 设备和系统设置应用程序
        caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("deviceName", deviceName); // 自动获取设备名称
        caps.setCapability("platformVersion", platformVersion);
        caps.setCapability("automationName", "XCUITest");
        caps.setCapability("udid", udid);
        caps.setCapability("bundleId", "com.zebra.messenger");
        caps.setCapability("usePrebuiltWDA", true);
        caps.setCapability("noReset", true);

        // 创建驱动对象
        driver = new AppiumDriver(url, caps);

        // 创建 WebDriverWait 对象，并指定等待时间显式等待时间
        wait = new WebDriverWait(driver, Duration.ofSeconds(EXPLICITTIME));
    }

    //shell类用来获取udid和设备名称和当前系统版本
    private String runCommand(String command) {
        StringBuilder output = new StringBuilder();
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line);
            }
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return output.toString().trim();
    }

    private String getDeviceUdid() {
        return runCommand("idevice_id -l");
    }

    private String getPlatformVersion() {
        return runCommand("ideviceinfo -k ProductVersion");
    }

    private String getDeviceName() {
        return runCommand("ideviceinfo -k DeviceName");
    }
}

















//单设备手动输入版本
//public class  Phione {
//    private   URL url = null;
//    private   DesiredCapabilities caps = null;
//    public  AppiumDriver driver;
//    public  WebDriverWait wait;
//
//    //显式等待时间
//    private  int EXPLICITTIME=15;
//
//
//    public Phione(){
//
//        try {
//            url = new URL("http://localhost:4723");
//        } catch (MalformedURLException e) {
//            throw new RuntimeException(e);
//        }
//
//
//////        // 配置 iOS 设备和系统设置应用程序
//        caps = new DesiredCapabilities();
//        caps.setCapability("platformName", "iOS");
//        caps.setCapability("deviceName", "iPhone 11");
//
//        caps.setCapability("platformVersion", "16.7");
//        caps.setCapability("automationName","xcuitest");
//        caps.setCapability("udid", "00008030-000C786A2E09802E");
//
//        caps.setCapability("bundleId", "com.zebra.messenger");
////            caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//        caps.setCapability("usePrebuiltWDA", true);// 替换为你要测试的应用程序的 bundle identifier
//        caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//
//
//
//
//
//
//
//
//
//        // 创建驱动对象
//        driver = new IOSDriver(url, caps);
//
//        // 创建 WebDriverWait 对象，并指定等待时间显式等待时间
//        wait = new WebDriverWait(driver, Duration.ofSeconds(EXPLICITTIME));
//    }
//}







//多设备测试版

//appium --use-plugin=images --port 4723
//appium --use-plugin=images --port 4725

//package auto.device;
//
//import io.appium.java_client.AppiumDriver;
//import io.appium.java_client.ios.IOSDriver;
//
//import org.openqa.selenium.remote.DesiredCapabilities;
//import org.openqa.selenium.support.ui.WebDriverWait;
//
//import java.net.MalformedURLException;
//import java.net.URL;
//import java.time.Duration;
//
//public class  Phione {
//    private   URL url1 = null;
//    private   URL url2 = null;
//    private   DesiredCapabilities caps1 = null;
//    private   DesiredCapabilities caps2 = null;
//    public  AppiumDriver driver1;
//    public  AppiumDriver driver2;
//
//
//
//
//    public Phione(){
//
//        try {
//            url1 = new URL("http://localhost:4723");
//        } catch (MalformedURLException e) {
//            throw new RuntimeException(e);
//        }
//
//        try {
//            url2 = new URL("http://localhost:4725");
//        } catch (MalformedURLException e) {
//            throw new RuntimeException(e);
//        }
//
//
//
////        // 配置 iOS 设备和系统设置应用程序
//        caps1 = new DesiredCapabilities();
//        caps1.setCapability("platformName", "iOS");
//        caps1.setCapability("deviceName", "iPhone XS Max");
//
//        caps1.setCapability("platformVersion", "16.0");
//        caps1.setCapability("automationName","xcuitest");
//        caps1.setCapability("udid", "00008020-000E5D363A68002E");
//
//        caps1.setCapability("bundleId", "com.zebra.messenger");
////            caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//        caps1.setCapability("usePrebuiltWDA", true);// 替换为你要测试的应用程序的 bundle identifier
//        caps1.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//        caps1.setCapability("wdaLocalPort", 8100);
//
//
//
//
//
//        caps2 = new DesiredCapabilities();
//        caps2.setCapability("platformName", "iOS");
//        caps2.setCapability("deviceName", "iPhone 11");
//
//        caps2.setCapability("platformVersion", "16.7");
//        caps2.setCapability("automationName","xcuitest");
//        caps2.setCapability("udid", "00008030-000C786A2E09802E");
//
//        caps2.setCapability("bundleId", "com.zebra.messenger");
////            caps.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//        caps2.setCapability("usePrebuiltWDA", true);// 替换为你要测试的应用程序的 bundle identifier
//        caps2.setCapability("noReset", true);//这个的意思是关闭驱动程序以后保留之前的数据
//        caps2.setCapability("wdaLocalPort", 8101);
//
//
//
//
//        // 创建驱动对象
//        driver1 = new IOSDriver(url1, caps1);
//        driver2 = new IOSDriver(url2, caps2);
//
//    }
//}

